package org.framework.edge.builder.infrastructure.persistence;

import jakarta.annotation.Resource;
import org.framework.edge.builder.domain.model.docker.repository.DockerRepository;
import org.framework.edge.builder.domain.model.docker.repository.DockerRepositoryRepository;
import org.framework.edge.builder.infrastructure.converter.DockerRepositoryConverter;
import org.framework.edge.builder.infrastructure.entity.DockerRepositoryDO;
import org.springframework.stereotype.Repository;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazySetValueWrappers;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * describe Docker 仓库
 *
 * @author Jia wei Wu
 * @date 2024/08/13 07:39 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class DockerRepositoryRepositoryImpl implements DockerRepositoryRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增Docker 仓库
     *
     * @param dockerRepository 新增Docker 仓库
     * @return {@link Result<DockerRepository>} Docker 仓库新增后领域对象
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<DockerRepository> story(DockerRepository dockerRepository) {
        DockerRepositoryDO dockerRepositoryDO = DockerRepositoryConverter.INSTANCE.fromDockerRepository(dockerRepository);
        lazyLambdaStream.upsert(dockerRepositoryDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增Docker 仓库
     *
     * @param dockerRepositoryList 批量新增Docker 仓库
     * @return {@link Result<List<DockerRepository>>} Docker 仓库新增后领域对象集合
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<List<DockerRepository>> batchStory(List<DockerRepository> dockerRepositoryList) {
        List<DockerRepositoryDO> dockerRepositoryDOList = dockerRepositoryList.stream().map(DockerRepositoryConverter.INSTANCE::fromDockerRepository).collect(Collectors.toList());
        lazyLambdaStream.upsert(dockerRepositoryDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个Docker 仓库
     *
     * @param dockerRepository 查询单个Docker 仓库
     * @return {@link Result<DockerRepository>} Docker 仓库领域对象
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<DockerRepository> findOne(DockerRepository dockerRepository) {
        DockerRepositoryDO dockerRepositoryDO = DockerRepositoryConverter.INSTANCE.fromDockerRepository(dockerRepository);
        DockerRepository dockerRepositoryOne = lazyLambdaStream.selectOne(
                LazyWrappers.<DockerRepositoryDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryDO::getCreateTime, dockerRepositoryDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryDO::getId, dockerRepositoryDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsActive, dockerRepositoryDO.getIsActive())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsDefault, dockerRepositoryDO.getIsDefault())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryDO::getPassword, dockerRepositoryDO.getPassword())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUpdateTime, dockerRepositoryDO.getUpdateTime())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUrl, dockerRepositoryDO.getUrl())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUsername, dockerRepositoryDO.getUsername())
                , DockerRepository.class);
        return ResultFactory.successOf(dockerRepositoryOne);
    }

    /**
     * describe 查询多个Docker 仓库
     *
     * @param dockerRepository 查询多个Docker 仓库
     * @return {@link Result<List<DockerRepository>>} Docker 仓库领域对象
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<List<DockerRepository>> findList(DockerRepository dockerRepository) {
        DockerRepositoryDO dockerRepositoryDO = DockerRepositoryConverter.INSTANCE.fromDockerRepository(dockerRepository);
        List<DockerRepository> dockerRepositoryList = lazyLambdaStream.selectList(
                LazyWrappers.<DockerRepositoryDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryDO::getCreateTime, dockerRepositoryDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryDO::getId, dockerRepositoryDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsActive, dockerRepositoryDO.getIsActive())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsDefault, dockerRepositoryDO.getIsDefault())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryDO::getPassword, dockerRepositoryDO.getPassword())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUpdateTime, dockerRepositoryDO.getUpdateTime())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUrl, dockerRepositoryDO.getUrl())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUsername, dockerRepositoryDO.getUsername())
                , DockerRepository.class);
        return ResultFactory.successOf(dockerRepositoryList);
    }

    /**
     * describe 分页查询多个Docker 仓库
     *
     * @param size             当前页数
     * @param current          当前页
     * @param dockerRepository 分页查询多个Docker 仓库
     * @return {@link Result<LazyPage<DockerRepository>>} 分页Docker 仓库领域对象
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<LazyPage<DockerRepository>> findPage(int size, int current, DockerRepository dockerRepository) {
        DockerRepositoryDO dockerRepositoryDO = DockerRepositoryConverter.INSTANCE.fromDockerRepository(dockerRepository);
        LazyPage<DockerRepository> lazyPage = new LazyPage<>(current, size);
        LazyPage<DockerRepository> dockerRepositoryLazyPage = lazyLambdaStream.selectPage(
                LazyWrappers.<DockerRepositoryDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryDO::getCreateTime, dockerRepositoryDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryDO::getId, dockerRepositoryDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsActive, dockerRepositoryDO.getIsActive())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsDefault, dockerRepositoryDO.getIsDefault())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryDO::getPassword, dockerRepositoryDO.getPassword())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUpdateTime, dockerRepositoryDO.getUpdateTime())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUrl, dockerRepositoryDO.getUrl())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUsername, dockerRepositoryDO.getUsername())
                , lazyPage, DockerRepository.class);
        return ResultFactory.successOf(dockerRepositoryLazyPage);
    }

    /**
     * describe 删除Docker 仓库
     *
     * @param dockerRepository 删除Docker 仓库
     * @return {@link Result<DockerRepository>} Docker 仓库
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<DockerRepository> remove(DockerRepository dockerRepository) {
        DockerRepositoryDO dockerRepositoryDO = DockerRepositoryConverter.INSTANCE.fromDockerRepository(dockerRepository);
        //  lazyLambdaStream.delete(LazyWrappers.lambdaWrapperBean(dockerRepositoryDO));
        lazyLambdaStream.update(
                LazySetValueWrappers
                        .<DockerRepositoryDO>lambdaWrapper()
                        .set(DockerRepositoryDO::getIsDeleted, true),
                LazyWrappers.<DockerRepositoryDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryDO::getCreateTime, dockerRepositoryDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryDO::getId, dockerRepositoryDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsActive, dockerRepositoryDO.getIsActive())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsDefault, dockerRepositoryDO.getIsDefault())
                        .eqIgnoreEmpty(DockerRepositoryDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryDO::getPassword, dockerRepositoryDO.getPassword())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUpdateTime, dockerRepositoryDO.getUpdateTime())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUrl, dockerRepositoryDO.getUrl())
                        .eqIgnoreEmpty(DockerRepositoryDO::getUsername, dockerRepositoryDO.getUsername())
        );
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在Docker 仓库
     *
     * @param dockerRepository Docker 仓库领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<Boolean> exists(DockerRepository dockerRepository) {
        DockerRepositoryDO dockerRepositoryDO = DockerRepositoryConverter.INSTANCE.fromDockerRepository(dockerRepository);
        Boolean exists = lazyLambdaStream.exists(LazyWrappers.lambdaWrapperBean(dockerRepositoryDO));
        return ResultFactory.successOf(exists);
    }

}